﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using jTCodeGenerate.Models;
using System.IO;
using jTCodeGenerate.Command;

namespace jTCodeGenerate
{
    public partial class SaveTemplateFrom : Form
    {
        public SaveTemplateFrom()
        {
            InitializeComponent();
            var data = SQLCommand.GetSolutionAll();
            this.cmb_solution.DataSource = data;
            this.cmb_solution.DisplayMember = "Name";
            this.cmb_solution.ValueMember = "Id";
            this.cmb_solution.SelectedIndex = -1;
        }

        private TemplateMdoel _templateMdoel;
        public SaveTemplateFrom(TemplateMdoel templateMdoel)
            : this()
        {
            _templateMdoel = templateMdoel;
            this.txt_template_path.Text = _templateMdoel.EntityGeneratePath;
            this.txt_entity_prefix.Text = _templateMdoel.EntityPrefix;
            this.txt_hl_entity_prefix.Text = _templateMdoel.IgnoreEntityPrefix;
            this.txt_entity_suffix.Text = _templateMdoel.EntitySuffix;
            this.txt_hl_entity_suffix.Text = _templateMdoel.IgnoreEntitySuffix;
            this.txt_hl_table_name_prefix.Text = _templateMdoel.IgnoreTableNamePrefix;
            this.txt_hl_table_name_suffix.Text = _templateMdoel.IgnoreTableNameSuffix;
            this.txt_table_name_prefix.Text = _templateMdoel.TableNamePrefix;
            this.txt_table_name_suffix.Text = _templateMdoel.TableNameSuffix;
            this.txt_key.Text = _templateMdoel.Key;
            this.txt_hl_field.Text = _templateMdoel.IgnoreField;
            this.txt_template_name.Text = _templateMdoel.DisplayName;
            this.txt_order.Text = _templateMdoel.Order.ToString();
            this.txt_alias.Text = _templateMdoel.Alias;
            this.txt_file_suffix.Text = _templateMdoel.FileSuffix;
            this.cmb_solution.SelectedValue = _templateMdoel.SolId;
            this.cmb_solution.SelectedText = _templateMdoel.SolName;

        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.cmb_solution.SelectedItem == null)
                {
                    MessageBox.Show("请选择生成方案！");
                    return;
                }
                var seleectItem = this.cmb_solution.SelectedItem as SolutionModel;
                if (_templateMdoel == null)
                {
                    var entityGeneratePath = this.txt_template_path.Text;
                    if (entityGeneratePath.Substring(entityGeneratePath.Length - 1).Equals("\\"))
                    {
                        entityGeneratePath = entityGeneratePath.Substring(0, entityGeneratePath.Length - 1);
                    }

                    var en = new TemplateMdoel
                    {
                        EntityGeneratePath = entityGeneratePath,
                        EntityPrefix = this.txt_entity_prefix.Text,
                        IgnoreEntityPrefix = this.txt_hl_entity_prefix.Text,
                        EntitySuffix = this.txt_entity_suffix.Text,
                        IgnoreEntitySuffix = this.txt_hl_entity_suffix.Text,
                        IgnoreTableNamePrefix = this.txt_hl_table_name_prefix.Text,
                        IgnoreTableNameSuffix = this.txt_hl_table_name_suffix.Text,
                        TableNamePrefix = this.txt_table_name_prefix.Text,
                        TableNameSuffix = this.txt_table_name_suffix.Text,
                        Key = this.txt_key.Text,
                        IgnoreField = this.txt_hl_field.Text,
                        DisplayName = this.txt_template_name.Text,
                        Name = $"{this.txt_template_name.Text}_{DateTime.Now.ToString("yyyyMMddHHmmss")}.cshtml",
                        RelativePath = "Templates",
                        Order = this.txt_order.Text.ToLogn(),
                        Alias = this.txt_alias.Text,
                        FileSuffix = this.txt_file_suffix.Text,
                        SolId = seleectItem.Id,
                        SolName = seleectItem.Name
                    };

                    SQLCommand.AddTemplate(en);
                }
                else
                {
                    var entityGeneratePath = this.txt_template_path.Text;
                    if (entityGeneratePath.Substring(entityGeneratePath.Length - 1).Equals("\\"))
                    {
                        entityGeneratePath = entityGeneratePath.Substring(0, entityGeneratePath.Length - 1);
                    }
                    _templateMdoel.EntityGeneratePath = entityGeneratePath;
                    _templateMdoel.EntityPrefix = this.txt_entity_prefix.Text;
                    _templateMdoel.IgnoreEntityPrefix = this.txt_hl_entity_prefix.Text;
                    _templateMdoel.EntitySuffix = this.txt_entity_suffix.Text;
                    _templateMdoel.IgnoreEntitySuffix = this.txt_hl_entity_suffix.Text;
                    _templateMdoel.IgnoreTableNamePrefix = this.txt_hl_table_name_prefix.Text;
                    _templateMdoel.IgnoreTableNameSuffix = this.txt_hl_table_name_suffix.Text;
                    _templateMdoel.TableNamePrefix = this.txt_table_name_prefix.Text;
                    _templateMdoel.TableNameSuffix = this.txt_table_name_suffix.Text;
                    _templateMdoel.Key = this.txt_key.Text;
                    _templateMdoel.IgnoreField = this.txt_hl_field.Text;
                    _templateMdoel.DisplayName = this.txt_template_name.Text;
                    _templateMdoel.Order = this.txt_order.Text.ToLogn();
                    _templateMdoel.Alias = this.txt_alias.Text;
                    _templateMdoel.FileSuffix = this.txt_file_suffix.Text;

                    _templateMdoel.SolId = seleectItem.Id;
                    _templateMdoel.SolName = seleectItem.Name;

                    SQLCommand.EditTemplate(_templateMdoel);
                }

                MessageBox.Show("保存成功！");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"{ex}");
            }
        }

        private void btn_sel_save_path_Click(object sender, EventArgs e)
        {
            var form = new SelectProjectItemForm();
            form.BtnOkClick = (node, frm) =>
            {
                string path = string.Empty;
                GetNodePath(node, ref path);
                this.txt_template_path.Text = path;
                frm.Close();
            };
            form.ShowDialog();
        }

        private void GetNodePath(TreeNode node, ref string path)
        {
            if (node == null)
                return;
            path = $"{node.Text}\\{path}";
            GetNodePath(node.Parent, ref path);
        }
    }
}
